package site.wetsion.employ.server.interceptor;

import java.math.BigInteger;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView;

import site.wetsion.employ.server.pojo.CollectionJob;
import site.wetsion.employ.server.pojo.User;
import site.wetsion.employ.server.service.UserService;

public class JobDetailInterceptor implements HandlerInterceptor {
	
	@Autowired
	private UserService userService;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		Map map = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("curr_user");
		if(user == null){
			return;
		}
		String id = (String) map.get("id");
		BigInteger jobid = new BigInteger(id);
		CollectionJob collectionJob = userService.getCollectionJob(user.getId(), jobid);
		System.out.println("jobid="+jobid);
		modelAndView.getModel().put("collection", collectionJob);
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {

	}

}
